package com.controller.manager;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.service.ConstructionService;

@Controller
@RequestMapping("/construction")
public class ConstructionController {

	@Resource
	private ConstructionService constructionService;

	@RequestMapping(value = "/toConstructionPage")
	public ModelAndView toConstructionPage(HttpServletRequest request) {
		String username = (String) request.getSession().getAttribute("username");
		if (username == null || "".equals(username)) {
			return new ModelAndView("/login");
		}
		return new ModelAndView("/manager/construction");
	}

	@RequestMapping(value = "/addConstructionInfo", method = RequestMethod.POST)
	@ResponseBody
	public String addConstructionInfo(@RequestParam Map<String, String> paramsMap) {
		int result = constructionService.addConstructionInfo(paramsMap);
		try {
			if (result > 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	// 删除
	@RequestMapping(value = "/deleteConstructionInfo", method = RequestMethod.POST)
	@ResponseBody
	public String deleteConstructionInfo(@RequestParam Map<String, String> paramsMap) {
		try {
			int result = constructionService.deleteConstructionInfo(paramsMap);
			if (result >= 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	@RequestMapping(value = "/updateConstructionInfo", method = RequestMethod.POST)
	@ResponseBody
	public String updateConstructionInfo(@RequestParam Map<String, String> paramsMap) {
		try {

			int result = constructionService.updateConstructionInfo(paramsMap);
			if (result > 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	// 查询
	@RequestMapping(value = "/getConstructionInfo", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, String>> getConstructionInfo(@RequestParam Map<String, String> paramsMap) {
		try {
			List<Map<String, String>> list = constructionService.getConstructionInfo(paramsMap);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@RequestMapping(value = "/getConstruction", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView getConstruction(HttpServletRequest request, @RequestParam Map<String, String> paramsMap) {
		List<Map<String, String>> list = constructionService.getConstructionByUuid(paramsMap);
		request.setAttribute("construction", list);
		return new ModelAndView("/manager/index.three");
	}

}
